//
//  MessageDM.m
//  WGFGlobalHouses
//
//  Created by Andy on 15/7/20.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MessageDM.h"
#import "MessageModel.h"


@implementation MessageDM



+(void)saveMessage:(MessageModel*)mod{
    
    
    [self setMsgType:mod.messageType numberNotRead:[self getMsgNotReadCountType:mod.messageType]+1];
    
    
    NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    
    if (mod.messageType ==MessageTypeSYS) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"sys"];
        
        NSMutableArray *arrya =  [NSMutableArray arrayWithArray:[dd arrayForKey:key]];
        [arrya insertObject:[mod toDic] atIndex:0];
        if (arrya.count>50) {
            [arrya removeLastObject];
        }
        [dd setObject:arrya forKey:key];
        
    }else if (mod.messageType==MessageTypeACTIVITY){
        
        NSString *key = [NSString stringWithFormat:@"%@",@"act"];
        
        NSMutableArray *arrya =  [NSMutableArray arrayWithArray:[dd arrayForKey:key]];
        [arrya insertObject:[mod toDic] atIndex:0];
        if (arrya.count>50) {
            [arrya removeLastObject];
        }
        [dd setObject:arrya forKey:key];
        
    }else if (mod.messageType==MessageTypeORDER) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"order"];
        
        NSMutableArray *arrya =  [NSMutableArray arrayWithArray:[dd arrayForKey:key]];
        [arrya insertObject:[mod toDic] atIndex:0];
        if (arrya.count>50) {
            [arrya removeLastObject];
        }
        [dd setObject:arrya forKey:key];
    }
    
    
    
    [dd synchronize];
}



+(NSArray *)getMessage:(MessageType)type{
    
    
    NSArray *array;
    NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    
    if (type==MessageTypeSYS) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"sys"];
        
        array =  [dd arrayForKey:key];
    }else if (type==MessageTypeACTIVITY){
        
        NSString *key = [NSString stringWithFormat:@"%@",@"act"];
        
        array = [dd arrayForKey:key];
        
    }else if (type==MessageTypeORDER){
        
        NSString *key = [NSString stringWithFormat:@"%@",@"order"];
        
        array = [dd arrayForKey:key];
    }
    
    NSMutableArray *modArray = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in array) {
        [modArray addObject:[[MessageModel alloc]initWithDic:dic]];
    }
    
    return modArray;
}



+(void)setMsgType:(MessageType)type numberNotRead:(NSInteger)count{
    
     NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    
    if (type ==MessageTypeSYS) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"sys_not_read"];
        [dd setInteger:count forKey:key];
        
    }else if (type==MessageTypeACTIVITY){
        
        NSString *key = [NSString stringWithFormat:@"%@",@"act_not_read"];
        [dd setInteger:count forKey:key];
        
    }else if (type==MessageTypeORDER) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"order_not_read"];
        [dd setInteger:count forKey:key];
    }
    

    [dd synchronize];
}




+(NSInteger)getMsgNotReadCountType:(MessageType)type{
    
    NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    
    if (type ==MessageTypeSYS) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"sys_not_read"];
        NSInteger count = [dd integerForKey:key] ;
        
        return count;
        
        
    }else if (type==MessageTypeACTIVITY){
        
        NSString *key = [NSString stringWithFormat:@"%@",@"act_not_read"];
        
        NSInteger count = [dd integerForKey:key] ;
        
        return count;
        
    }else if (type==MessageTypeORDER) {
        
        NSString *key = [NSString stringWithFormat:@"%@",@"order_not_read"];
        NSInteger count = [dd integerForKey:key] ;
        
        return count;
    }
    
    return 0;
}





+(NSInteger)getMessageNotReadCount{

    return [[self class] getMsgNotReadCountType:MessageTypeACTIVITY]+[[self class] getMsgNotReadCountType:MessageTypeORDER]+[[self class] getMsgNotReadCountType:MessageTypeSYS];
}








@end
